using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Triggers
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TimerTrigger : WorkflowTriggerBase
    {
        /// <summary>
        /// 
        /// </summary>
        public TimerTrigger():base()
        {
            this._TimerTriggerOutput=new TimerTriggerOutput(this.ComponentID,Guid.Empty,DateTime.Now);
        }

        private TimerTriggerOutput _TimerTriggerOutput;
        public override ResultBase Output
        {
            get
            {
                return this._TimerTriggerOutput;
            }
            set
            { }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode timerNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, timerNode, "WorkflowTriggerType", ((int) this.TriggerType).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, timerNode, "TimeSpanUnit", ((int) this.TimeSpanUnit).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, timerNode, "Interval", this.Interval.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, timerNode, "FirstStartTime", this.StartTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, timerNode, "LastRunTime", this.LastRunTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, timerNode, "NextRunTime", this.NextRunTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, timerNode, "IsActive", this.IsActive.ToString());
            return timerNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if(xNode.Attributes.GetNamedItem("Type").Value==typeof(TimerTrigger).FullName)
            {
                TimerTrigger timer = base.Instantiate(xNode) as TimerTrigger;
                timer.TriggerType =
                    (WorkflowTriggerType) int.Parse(xNode.Attributes.GetNamedItem("WorkflowTriggerType").Value);
                timer.TimeSpanUnit = (TimeLapseUnit) int.Parse(xNode.Attributes.GetNamedItem("TimeSpanUnit").Value);
                timer.Interval = int.Parse(xNode.Attributes.GetNamedItem("Interval").Value);
                timer.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FirstStartTime").Value);
                timer.LastRunTime = DateTime.Parse(xNode.Attributes.GetNamedItem("LastRunTime").Value);
                timer.NextRunTime = DateTime.Parse(xNode.Attributes.GetNamedItem("NextRunTime").Value);
                timer.IsActive = bool.Parse(xNode.Attributes.GetNamedItem("IsActive").Value);

                return timer;    
            }
            else 
                throw new Exception("Unable to instantiate Timer Trigger: invalid type specified in xml node");
            
        }

        //public override TimerTriggerOutput CheckTriggerState()
        //{
        //    TimerTriggerOutput output = new TimerTriggerOutput();
        //    throw new NotImplementedException();
        //}
        public override ResultBase[] ProcessTrigger(DateTime lastRunTime)
        {
            this._TimerTriggerOutput = new TimerTriggerOutput(this.ComponentID, Guid.Empty, DateTime.Now);
            return new TimerTriggerOutput[] {this._TimerTriggerOutput};
        }
    }
}
